import { ElMessage } from "element-plus";

import { computed } from "vue";
const baseURL = "https://c.u7games.com/";
const timestamp = new Date().getTime();

//输出 1591670068833   13位
export let useHttpGET = async (
  url: string,
  method: any = "GET",
  query = {}
) => {
  let res = await useFetch(url, {
    method,
    baseURL,
    key: timestamp.toString(),
    query,
    headers: { "Content-Type": "application/json" },
    onRequest(config: any) {
      // 设置请求头
      config.options.headers = config.options.headers || {};
      let token = localStorage.getItem("token");
      if (token) {
        config.options.headers["Authorization"] = "Bearer " + token;
      }
      // options.headers.authorization = '...'
    },
    onRequestError({ request, options, error }) {
      // 处理请求错误
    },
    onResponse({ request, response, options }) {
      // 处理响应数据
      //   localStorage.setItem("token", response._data.token);
    },
    onResponseError({ request, response, options }) {
      // 处理响应错误
    },
  });
  return res;
};

export let useHttpPOST = async (url: string, body = {}) => {
  let res = await useFetch(url, {
    method: "POST",
    baseURL,
    key: timestamp.toString(),
    body,
    headers: { "Content-Type": "application/json" },
    onRequest(config: any) {
      // 设置请求头
      let store = useStore();
      config.options.headers = config.options.headers || {};
      let token = computed(() => store.token);
      if (token.value != "") {
        config.options.headers["Authorization"] = "Bearer " + token.value;
      }
      // options.headers.authorization = '...'
    },
    onRequestError({ request, options, error }) {
      // 处理请求错误
    },
    onResponse({ request, response, options }) {
      // 处理响应数据
      //   localStorage.setItem("token", response._data.token);
    },
    onResponseError({ request, response, options }) {
      // 处理响应错误
    },
  });
  return res;
};
export let useOtherPOST = async (url: string, body = {}) => {
  let res = await useFetch(url, {
    method: "POST",
    baseURL: "/api",
    key: timestamp.toString(),
    body,
    headers: { "Content-Type": "application/json" },
    onRequest(config: any) {
      // 设置请求头
      config.options.headers = config.options.headers || {};
      let token = localStorage.getItem("token");
      if (token) {
        config.options.headers["Authorization"] = "Bearer " + token;
      }
      // options.headers.authorization = '...'
    },
    onRequestError({ request, options, error }) {
      // 处理请求错误
    },
    onResponse({ request, response, options }) {
      // 处理响应数据
      //   localStorage.setItem("token", response._data.token);
    },
    onResponseError({ request, response, options }) {
      // 处理响应错误
    },
  });
  return res;
};
type AsyncDataOptions = {
  server?: boolean;
  lazy?: boolean;
  immediate?: boolean;
  deep?: boolean;
  default?: any;
  transform?: any;
  pick?: string[];
  watch?: any;
  getCachedData?: any;
};

let MYFetch = (url: string, data: any = {}) => {
  return $fetch(url, {
    method: "POST",
    body: {
      ...data,
    },
    baseURL: baseURL,
    headers: { "Content-Type": "application/json" },
    onRequest(config: any) {
      // 设置请求头
      config.options.headers = config.options.headers || {};
      let token = localStorage.getItem("token");
      if (token) {
        config.options.headers["Authorization"] = "Bearer " + token;
      }
      // options.headers.authorization = '...'
    },
    onRequestError() {
      // 处理请求错误
    },
    onResponse() {
      // 处理响应数据
      //   localStorage.setItem("token", response._data.token);
    },
    onResponseError() {
      // 处理响应错误
    },
  });
};
export let useAllData = async (key: string, url: string, data: any) => {
  return useAsyncData(key, () => MYFetch(url, data), {});
};
